				/*		Polarization project		
				*/

				
/*clear register */
clear
clear mata
clear matrix
label drop _all

set maxvar 30000



*Read
use  "V-Dem-CY-Full+Others-v13.dta", replace

		

	keep if year >= 2000
		
		
	keep if (inlist(country_text_id, "USA", "COL", "MEX", "ARG", "CHL", "BRA", "BOL", "PER") | inlist(country_text_id, "ECU", "VEN")) | country_name == "Nigeria"	| country_name == "Ivory Coast"	| country_name == "Madagascar"	| country_name == "Ghana" | country_name == "Tanzania"	|  country_name == "South Africa" | country_name == "Kenya"	| country_name == "Niger" |	country_name == "Mozambique"	|	country_name == "India" |	country_name == "Indonesia"  |	country_name == "Pakistan" | country_name == "Bangladesh" |	country_name == "Japan" |   country_name == "Philippines" | country_name == "Turkey" | country_name == "Thailand" | country_name == "South Korea"

	keep country_name country_id country_text_id year country_id v2cacamp* v2smpolsoc* v2mebias* v2smonex* v2smmefra* v2xedvd_me_cent* v2medstateprint* v2medstatebroad* v2medpolstate* v2x_polyarchy* v2x_libdem* e_democ e_chga_demo e_fh_status
	
*Declare panel data
	encode country_name, gen(countyID)
	xtset countyID year
	
	drop *codelow *codehigh
	
	
*Create regions
	gen LatinAmerica = 0 if country_text_id == "USA"
	replace LatinAmerica =1 if inlist(country_text_id, "COL", "MEX", "ARG", "CHL", "BRA", "BOL", "PER") | inlist(country_text_id, "VEN", "ECU")
	
	replace LatinAmerica = 2 if country_name == "Nigeria"	| country_name == "Ivory Coast"	| country_name == "Madagascar"	| country_name == "Ghana" | country_name == "Tanzania"	|  country_name == "South Africa" | country_name == "Kenya"	| country_name == "Niger" |	country_name == "Mozambique"
	
	replace LatinAmerica = 3 if country_name == "India" |	country_name == "Indonesia"  |	country_name == "Pakistan" |	country_name == "Bangladesh" |	country_name == "Japan" |   country_name == "Philippines" | country_name == "Turkey" | country_name == "Thailand" | country_name == "South Korea"

*Recode	polarization of society
foreach var of varlist v2smpolsoc v2smmefra {
		clonevar recoded_`var' = `var'
		replace recoded_`var' = `var'*-1
	}	
	
*Collapse
	collapse (mean) v2x_libdem v2cacamps recoded_v2smpolsoc (firstnm) country_name, by(LatinAmerica year)
	
*Change name	
	replace country_name = "Latin America" if LatinAmerica ==1
	replace country_name = "Africa" if LatinAmerica ==2
	replace country_name = "Asia" if LatinAmerica ==3
	replace country_name = "United States" if LatinAmerica ==0
	encode country_name, gen(countyID)
	
*Declare as panel data 	
	xtset countyID year
	

*Graph evolution of political polarization and democracy
	xtline v2cacamps recoded_v2smpolsoc, byopt(note("")) addplot(line v2x_libdem year, yaxis(2) r1("Democracy index", size(small))) color(ebblue) ytitle("Polarization") legend(label(1 "Political polarization") label(2 "Social polarization") label(3 "Liberal democracy") col(3)) xlabel(, labsize(small))
	graph export "Political_polarization_Democracy_All.png", replace




	
